define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'staff/index' + location.search,
                    add_url: 'staff/add',
                    edit_url: 'staff/edit',
                    del_url: 'staff/del',
                    multi_url: 'staff/multi',
                    import_url: 'staff/import',
                    table: 'staff',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                search:false,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'gender', title: __('Gender'), searchList: {"1":__('Gender 1'),"2":__('Gender 2')}, formatter: Table.api.formatter.normal},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'idcard', title: __('Idcard'), operate: 'LIKE'},
                        {field: 'currentAddress', title: __('CurrentAddress'), operate: 'LIKE'},
                        {field: 'permanentAddress', title: __('PermanentAddress'), operate: 'LIKE'},
                        // {field: 'urgent_name', title: __('Urgent_name'), operate: 'LIKE'},
                        // {field: 'urgent_relation', title: __('Urgent_relation'), operate: 'LIKE'},
                        // {field: 'urgent_mobile', title: __('Urgent_mobile'), operate: 'LIKE'},
                        // {field: 'bank_cardnum', title: __('Bank_cardnum'), operate: 'LIKE'},
                        {field: 'bankName', title: __('BankName'), operate: 'LIKE'},
                        {field: 'company_name', title: __('Company_id'), operate: false},
                        {field: 'company_id', title: __('Company_id'), visible:false, searchList:$.getJSON('company/getcompanylist')},
                        {field: 'contract_status', title: __('Contract_status'), searchList: {"0":__('ContractStatus 0'),"1":__('ContractStatus 1'),"2":__('ContractStatus 2'),"3":__('ContractStatus 3'),"4":__('ContractStatus 4'),"5":__('ContractStatus 5'),"6":__('ContractStatus 6'),"7":__('ContractStatus 7'),"8":__('ContractStatus 8'),"9":__('ContractStatus 9'),"10":__('ContractStatus 10'),"21":__('ContractStatus 21')}, formatter: Table.api.formatter.normal},
                        {field: 'contract_end_date', title: __('合同到期时间'),operate: 'RANGE', addclass: 'datetimerange',data:'autocomplete="off" data-locale={"format":"YYYY-MM-DD"}'},
                        // {field: 'contract_end_date', style:'width:290px', title: "合同到期时间", operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.date},
                        {field: 'createtime', style:'width:290px', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'flowid', title: __('Flowid'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'),
                                table: table,
                                buttons: [
                                    {
                                        name: "contract",
                                        text: "合同管理",//按钮名称
                                        classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                        url: 'staff/contract',//指向控制器对应方法
                                        visible: function (row) {
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        },
                                        success: function (data, ret) {
                                            console.log(1,data, ret);
                                            //如果需要阻止成功提示，则必须使用return false;
                                            //return false;

                                        },
                                        error: function (data, ret) {
                                            console.log(2,data, ret);
                                            Layer.alert(ret.msg);
                                            return false;
                                        },
                                    },
                                ],
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'staff/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {field: 'mobile', title: __('Mobile'), align: 'left'},
                        {field: 'idcard', title: __('Idcard'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'staff/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'staff/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        contract: function () {

            var default_templateId = $('#default_templateId').val();

            // 公司选择
            $(document).on('change', '#company_id', function(e) {
                var company_id = $(this).val();

                let html = '<option value="">请选择</option>';
                $.post("staff/getTemplateIdListByCompanyId", {company_id:company_id}, function(res) {
                    if (res.length>0) {
                        $.each(res, function(i,v) {
                            if (default_templateId == v.value) {
                                html += '<option selected value="'+v.value+'">'+v.name+'</option>';
                            } else {
                                html += '<option value="'+v.value+'">'+v.name+'</option>';
                            }

                        });
                    }

                    $('#templateId').html(html);

                    $('#templateId').change();

                }, 'json');

            });

            $('#company_id').change();

            // 合同选择
            $(document).on('change', '#templateId', function(e) {
                var templateId = $(this).val();
                var html = '';
                if (templateId == 'yDtCrUUl7bvfqtUucGHGoG3jGmQvsw1O') { //退休返聘

                    $('#yinian').hide();

                    var xybh = $('#协议编号').val() != undefined ? $('#协议编号').val() : '';
                    var htqxjn = $('#合同期限几年').val() != undefined ? $('#合同期限几年').val() : '';
                    var ygz = $('#月工资').val() != undefined ? $('#月工资').val() : '';

                    var myjhfgz = $('#每月几号发工资').val() != undefined ? $('#每月几号发工资').val() : '';

                    html = '\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">协议编号:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][协议编号]" type="text" value="'+xybh+'">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">合同期限几年:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][合同期限几年]" type="number" value="'+htqxjn+'">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">月工资:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][月工资]" type="text" value="'+ygz+'">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">每月几号发工资:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][每月几号发工资]" type="number" value="'+myjhfgz+'">\
                                </div>\
                            </div>\
                            ';
                    $('.buchong').show();
                } else if (templateId == 'yDtCrUUl7unz64UEc3ZwnRueWCoLDPCk') { //厨保洁机场

                    $('#yinian').hide();

                    var htqxjn = $('#合同期限几年').val() != undefined ? $('#合同期限几年').val() : '';
                    var ygz = $('#月工资').val() != undefined ? $('#月工资').val() : '';
                    var syqygz = $('#试用期月工资').val() != undefined ? $('#试用期月工资').val() : '';
                    var myjhfgz = $('#每月几号发工资').val() != undefined ? $('#每月几号发工资').val() : '';

                    html = '\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">合同期限几年:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][合同期限几年]" type="text" value="'+htqxjn+'">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">月工资:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][月工资]" type="text" value="'+ygz+'">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">试用期月工资:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][试用期月工资]" type="text" value="'+syqygz+'">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="control-label col-xs-12 col-sm-2">每月几号发工资:</label>\
                                <div class="col-xs-12 col-sm-8">\
                                    <input id="c-other" data-rule="required" class="form-control" name="row[other_data][每月几号发工资]" type="number" value="'+myjhfgz+'">\
                                </div>\
                            </div>\
                            ';
                    $('.buchong').show();
                } else {
                    $('#yinian').show();
                    $('.buchong').hide();
                }

                $('#buchong_body').html(html);

                // 参数隐藏显示
                var arr = ['yDCYLUU5v3nc1jUqzfmxuBhJ6pT3jy8u', 'yDtCrUUl7unz64UEc3ZwnRueWCoLDPCk'];
                if (arr.includes(templateId)) {
                    $('.shiyongqi_div').show();
                } else {
                    $('.shiyongqi_div').hide();
                }
            })

            $('#templateId').change();

            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
